<template>
  <div class="card-list">
    <div class="v-card" v-for="(item, index) in list" :key="index">
      <el-card class="video-card">
        <div @click="selectItem(item)">
          <el-image :src="item.img | coverUrl" lazy class="pic"></el-image>
        </div>
        <div class="title">
          <span v-if="item.uid == uuid">【{{item.show | isShow}}】</span>
          <span>{{item.title}}</span>
        </div>
        <div v-if="item.uid == uuid" class="videoData">
          <el-button type="text" @click="modifyItem(item,index)">修改</el-button>
          <el-button type="text" @click="deleteVideo(item,index)">删除</el-button>
        </div>
      </el-card>
    </div>
    <el-dialog title="修改视频信息" :visible.sync="dialogFormVisible">
      <el-form :model="modifyForm" :rules="rules" ref="modifyForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="modifyForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="content">
          <el-input v-model="modifyForm.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="公开">
          <el-switch v-model="modifyForm.isShow"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <v-btn color="#fff" tile @click="dialogFormVisible = false">取 消</v-btn>
        <v-btn color="#2196f3" dark @click="beforemodifyVideo('modifyForm')">确 定</v-btn>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deleteVideo, modifyMyVideo } from "@/api/videoService";
import { coverUrl } from "@/utils/url";
export default {
  props: {
    list: {
      type: Array,
      default: null,
    },
  },
  data() {
    return {
      dialogFormVisible: false,
      index: "", //数组索引，用于修改后在页面中修改
      deleteId: {
        id: "",
      },
      modifyForm: {
        id: "",
        title: "",
        content: "",
        isShow: Boolean,
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入简介", trigger: "blur" }],
      },
    };
  },
  methods: {
    selectItem(item) {
      this.$emit("select", item);
    },
    deleteVideo(item, index) {
      this.deleteId.id = item.vid;
      deleteVideo(this.deleteId).then((res) => {
        if (res.data.code === 200) {
          this.$message.success(res.data.msg);
          this.list.splice(index, 1);
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    modifyItem(item, index) {
      this.modifyForm.id = item.vid;
      this.modifyForm.title = item.title;
      this.modifyForm.content = item.content;
      this.modifyForm.isShow = item.show;
      this.dialogFormVisible = true;
      this.index = index;
    },
    beforemodifyVideo(userForm) {
      this.$refs[userForm].validate((valid) => {
        if (valid) {
          this.modifyMyVideo();
        } else {
          this.$message.error("请检查输入的数据");
          return;
        }
      });
    },
    modifyMyVideo() {
      modifyMyVideo(this.modifyForm).then((res) => {
        if (res.data.code === 200) {
          this.$message.success("修改成功");
          this.dialogFormVisible = false;
          //修改本地数据
          this.list[this.index].title = this.modifyForm.title;
          this.list[this.index].content = this.modifyForm.content;
          this.list[this.index].show = this.modifyForm.isShow;
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
  },
  created() {
    this.uid = this.$store.state.userInfo.Uid;
  },
  computed: {
    uuid() {
      return this.$store.state.userInfo.Uid;
    },
  },
  filters: {
    coverUrl(name) {
      return coverUrl(name);
    },
    isShow(show) {
      if (show) {
        return "公开";
      } else {
        return "隐藏";
      }
    },
  },
};
</script>

<style>
.card-list {
  display: flex;
  flex-wrap: wrap;
  margin-top: 15px;
}

.v-card {
  display: inline-block;
  width: 30%;
  padding: 8px 5px;
  box-sizing: border-box;
  margin-left: 30px;
}

.video-card {
  height: 330px;
}

.pic {
  top: 0px;
  left: 0px;
  width: 100%;
  height: 240px;
}

.title {
  height: 20px;
  margin: 0;
  font-family: "Microsoft YaHei";
  font-size: 18px;
  line-height: 20px;
  color: #212121;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}

.videoData {
  float: right;
}
</style>